package com.sk.sourcecircle.activity.mine.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.find.act.SatisfyTaskDetailsActvitiy;
import com.sk.sourcecircle.activity.find.act.TaskDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.AblumDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.DetailsOfConsultationActivity;
import com.sk.sourcecircle.activity.home.activity.EventDetailsActivity;
import com.sk.sourcecircle.adapter.MyCollectionAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.dialog.TipDialogFragment;
import com.sk.sourcecircle.entity.AlbumaBean;
import com.sk.sourcecircle.entity.CollectListBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/23.
 */

public class MyCollectionActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemLongClickListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.edit)
    TextView edit;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private LoadingDialog loadingDialog;
    private int page = 1;
    private MyCollectionAdapter adapter;
    private List<CollectListBean.DataBeanX> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;

    private int pos;

    @Override
    protected int getContentView() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MyCollectionAdapter(allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnItemLongClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        loadingDialog.show();
        AppApi.myCollection(page, this, 0, this);
    }

    @OnClick({R.id.back, R.id.edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit:
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                CollectListBean collectListBean = JSON.parseObject(result, CollectListBean.class);
                List<CollectListBean.DataBeanX> dataBeanList = collectListBean.getData();
                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case 1:
                break;
            case 10:
                SnackUtils.show(mRootView,"删除成功！");
                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();

    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        switch (allDataList.get(position).getFavorite_type()) {
            case 1:
                if(allDataList.get(position).getData().getType() == 0){
                    Intent intent = new Intent(mContext, EventDetailsActivity.class);
                    intent.putExtra("id", allDataList.get(position).getData().getActivity_id());
                    startActivity(intent);
                }else if(allDataList.get(position).getData().getType() == 1&&allDataList.get(position).getData().getAc_status() == 0){
                    Intent intent = new Intent(mContext, TaskDetailsActivity.class);
                    intent.putExtra("id", allDataList.get(position).getData().getActivity_id());
                    startActivity(intent);
                }else if(allDataList.get(position).getData().getType() == 1&&allDataList.get(position).getData().getAc_status() == 1){
                    Intent intent = new Intent(mContext, SatisfyTaskDetailsActvitiy.class);
                    intent.putExtra("id", allDataList.get(position).getData().getActivity_id());
                    startActivity(intent);
                }
                break;
            case 2:
                Intent intent1 = new Intent(mContext, DetailsOfConsultationActivity.class);
                intent1.putExtra("id", allDataList.get(position).getData().getArticle_id());
                startActivity(intent1);
                break;
            case 3:
                Intent intent2 = new Intent(mContext, AblumDetailsActivity.class);
                CollectListBean.DataBeanX.DataBean dataBeanX = allDataList.get(position).getData();
                List<String> stringList = new ArrayList<>();
                if (dataBeanX.getImages() != null) {
                    String[] strings = dataBeanX.getImages().split(",");
                    stringList.addAll(Arrays.asList(strings));
                }

                AlbumaBean dataBean = new AlbumaBean();
                dataBean.setAlbum_id(dataBeanX.getAlbum_id());
                dataBean.setCommunity_id(dataBeanX.getCommunity_id());
                dataBean.setCreate_time(dataBeanX.getCreate_time());
                dataBean.setDetail(dataBeanX.getDetail());
                dataBean.setDig_count(dataBeanX.getDig_count());
                dataBean.setImages(stringList);
                dataBean.setReply_count(dataBeanX.getReply_count());
                dataBean.setShow_auth(dataBeanX.getShow_auth());
                dataBean.setRank(dataBeanX.getRank());
                intent2.putExtra("data", dataBean);
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.myCollection(page, this, 0, this);
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        pos = position;
        TipDialogFragment tipDialogFragment = new TipDialogFragment();
        tipDialogFragment.setContent("您想要删除该收藏嘛？");
        tipDialogFragment.setMyCallBack(MyCollectionActivity.this);
        tipDialogFragment.show(getSupportFragmentManager(), "cancel");
        return false;
    }

    @Override
    public void sure() {
        super.sure();

        switch (allDataList.get(pos).getFavorite_type()) {
            case 1:
                AppApi.cancelCollect(allDataList.get(pos).getData().getActivity_id(), 1, MyCollectionActivity.this, 10, MyCollectionActivity.this);
                break;
            case 2:
                AppApi.cancelCollect(allDataList.get(pos).getData().getArticle_id(), 2, MyCollectionActivity.this, 10, MyCollectionActivity.this);

                break;
            case 3:
                AppApi.cancelCollect(allDataList.get(pos).getData().getAlbum_id(), 3, MyCollectionActivity.this, 10, MyCollectionActivity.this);

                break;
        }
        allDataList.remove(pos);
        adapter.notifyItemRemoved(pos);
        loadingDialog.show();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()){
            case R.id.more:
                allDataList.get(position).getData().setExpand(true);
                adapter.notifyItemChanged(position);
                break;
        }
    }
}
